package com.annotation.configure;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @EnableTransactionManagement  开启事务管理功能
 */
@Configuration
@ComponentScan({"com.annotation"})
@EnableTransactionManagement
public class TestDataSourceConfig {

     @Bean
     public DataSource getDataSouce() throws PropertyVetoException {
         ComboPooledDataSource dataSource=new ComboPooledDataSource();
         dataSource.setUser("root");
         dataSource.setPassword("123456");
         dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
         dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true");
         return  dataSource;

     }

     @Bean
    public JdbcTemplate getJdbcTemplate() throws PropertyVetoException {
         JdbcTemplate jdbcTemplate=new JdbcTemplate(getDataSouce());
         return jdbcTemplate;
     }


    /**
     *    注册事务管理器
     * @return
     * @throws PropertyVetoException
     */
     @Bean
    public PlatformTransactionManager  getTransactionManager() throws PropertyVetoException {
         return  new DataSourceTransactionManager(getDataSouce());
     }
}
